<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Page;
use Org\Net\Http;
use Think\Image;

class StaffController extends AuthController {
	public function index() {
		$staff = D ( 'Staff' );
		$list = $staff->order ( array (
				'id' => 'asc' 
		) )->select ();
		$this->assign ( 'list', $list );
		$this->display ();
	}
	public function staffOrder(){
		$staff = I("staff");
		if($staff){
			$where = " where o.staff = '{$staff}'";
		}
		$list = M()->query("SELECT o.*,u.name,u.account,u.phone_number FROM vw_order o LEFT JOIN vw_user u ON o.uid=u.id {$where} ORDER BY o.id DESC");
		$otimes = M ( 'OrderTime' )->order ( array (
				'id' => 'asc'
		) )->select ();
		$this->assign ( 'list', $list );
		// 		file_put_contents("Public/Log/log.txt", "list:".json_encode($list)."\r\n",FILE_APPEND|LOCK_EX);
		$ptimes = array ();
		foreach ( $otimes as $otime ) {
			// 			if ($otime ['status']) {
			$ptime = "";
			switch ($otime ['type']) {
				case 1 :
					$ptime .= "当天";
					break;
				case 2 :
					$ptime .= "次日";
					break;
			}
			$ptime .= "{$otime['stime']}-{$otime['etime']}";
			$ptimes [] = $ptime;
			// 			}
		}
		$this->assign ( 'ptimes', $ptimes );
		$this->assign ( 'staff', I("staff") );
		$this->display ();
	}
	public function updateStaff() {
		if (IS_POST) {
			$staff = session ( "auth" );
			if (I ( 'npwd' ) == I ( 'rpwd' )) {
				if (md5 ( I ( 'opwd' ) ) == $staff ['password']) {
					$where ['id'] = $staff ['id'];
					$data ['password'] = md5 ( I ( 'npwd' ) );
					$result = M ( "Staff" )->where ( $where )->save ( $data );
					if ($result) {
						$this->success ( '修改成功', __APP__ . '/Home/Index/login' );
					} else {
						$this->error ( '修改失败' );
					}
				} else {
					$this->error ( '旧的密码输入不一致' );
				}
			} else {
				$this->error ( '两次密码输入不一致' );
			}
		} else {
			$this->display ();
		}
	}
	public function update() {
		if (IS_POST) {
			$where ['id'] = I ( 'id' );
			$data['name'] = I('name');
			$data['account'] = I('account');
			$password = I('password');
			if($password){
				$data ['password'] = md5 ( I ( "password" ) );
			}
			$staff = D ( 'Staff' );
			$result = $staff->where ( $where )->save ( $data );
			if ($result) {
				$this->success ( "修改成功", 'index' );
			} else {
				$this->error ( "修改失败" );
			}
		}
	}
	public function addStaff() {
		if (IS_POST) {
			$staff = D ( 'Staff' );
			$data = $staff->create ( $_POST );
			$result = $staff->add ( $data );
			if ($result) {
				$this->success ( "添加成功", "index" );
			} else {
				$this->error ( "添加失败" );
			}
		}
	}
}
?>